from app import db
from flask import Blueprint,jsonify,request
from application.models.users.user import classes,Kind


user_blue = Blueprint("userblue", __name__)




import math
@user_blue.route('/class')
def getclasses():
    # 获取当前页数
    page = int(request.args.get('page'))
    # 查询总条数
    total = classes.query.count()
    # 定义每页显示多少条
    page_seze = 3
    # 查询当前页记录
    start = math.ceil((page-1)*page_seze)
    cls = classes.query.offset(start).limit(page_seze).all()
    list1 = [{'id':i.id,'name':i.name,'kid':i.kind.name} for i in cls]
    return jsonify({'code':200,'msg':'ok','lst':list1,'total':total,'page_seze':page_seze})


@user_blue.route('/del/<id>',methods=['DELETE'])
def deletecls(id):
    classes.query.filter_by(id=id).delete()
    db.session.commit()
    return jsonify({'code':200,'msg':'删除成功'})

@user_blue.route('/upd/<id>',methods=['PUT'])
def updcls(id):
    data = request.json
    id = data.get('id')
    name = data.get('name')
    kid = data.get('kid')
    print('name>>>>>>>',name)
    print('kid>>>>>>>',kid)
    if not all([name,kid]):
        return jsonify({'code':400,'msg':'字段不完整'})
    cls = classes.query.filter_by(id=id).first()
    cls.name=name
    cls.kid=kid
    db.session.commit()
    return jsonify({'code':200,'msg':'修改成功'})